var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

require('./utils/database')

var studentsRouter = require('./routes/login_regist');
//引入路由文件
//登录注册修改
var authRouter = require('./routes/token');
var uploadRouter = require('./routes/upload');
const jwtAuth = require('./utils/auth.js');

//错题
var errorsRouter = require('./routes/errorsRouter');
var collectionsRouter = require('./routes/collectionsRouter');
var infosRouter = require('./routes/infosRouter');
var exercisesRouter = require('./routes/exercises');
var typesRouter = require('./routes/types');
var testRouter = require('./routes/test');
var testedRouter = require('./routes/tested');


var app = express();


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use(jwtAuth);
app.use('/students', studentsRouter);
app.use('/Upload', uploadRouter);
app.use('/Auth', authRouter);

app.use('/errors', errorsRouter);
app.use('/collections', collectionsRouter);
app.use('/infos', infosRouter);
app.use('/Exercise',exercisesRouter);
app.use('/Type',typesRouter);
app.use('/Test',testRouter);
app.use('/Tested',testedRouter);



app.use(function(req, res, next) {
  next(createError(404));
});


app.use(function(err, req, res, next) {
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
let port=3000;
app.listen(port,()=>{
  console.log(`服务已启动，端口号${port}`)
})
